<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" media="all" th:href="@{/admin/layui/css/layui.css}">
    <link rel="stylesheet" media="all" th:href="@{/admin/style/admin.css}">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <!-- 搜索 -->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" placeholder="请输入账号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">模块</label>
                    <div class="layui-input-block">
                        <select name="moduleType">
                            <option value="">请选择模块</option>
                            <option value="1">登录日志</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">操作日期</label>
                    <div class="layui-inline" id="dateStr">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="beginTime" name="beginTime" class="layui-input" placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="endDate" name="endDate" class="layui-input" placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn" data-type="search" lay-submit lay-filter="LAY-search">搜索
                    </button>
                </div>
            </div>
        </div>

        <!-- 操作 -->
        <div class="layui-card-body">
            <div style="padding-bottom: 20px;">
                <button class="layui-btn layuiadmin-btn" data-type="batchDel">删除</button>
            </div>
            <table id="lay-list" lay-filter="lay-list"></table>
            <script type="text/html" id="table-operation">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script th:src="@{/admin/layui/layui.js}"></script>
<script th:src="@{/webjars/JavaScript-MD5/js/md5.js}"></script>
<script>
    layui.config({
        base: '/admin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'admin', 'laydate'], function () {
        var form = layui.form, table = layui.table, admin = layui.admin, laydate = layui.laydate, $ = layui.$;

        //监听搜索
        form.on('submit(LAY-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('lay-list', {
                where: field
            });
        });

        /* 渲染时间选择 */
        laydate.render({
            elem: '#dateStr',
            type: 'date',
            range: ['#beginTime', '#endTime'],
            // range: true,
            trigger: 'click'
        });

        //列表管理
        table.render({
            elem: '#lay-list'
            , url: 'query'
            , method: 'post'
            , cols: [
                [
                    {type: 'numbers', width: 80, align: 'center', title: ''}
                    , {type: 'checkbox'}
                    , {field: 'username', width: 150, align: 'center', title: '账号'}
                    , {field: 'moduleTypeStr', width: 150, align: 'center', title: '模块'}
                    , {field: 'requestIp', width: 200, align: 'center', title: '访问ip'}
                    , {field: 'methodName', align: 'center', title: '方法路径'}
                    , {field: 'remark', align: 'center', title: '备注'}
                    , {field: 'createTime', align: 'center', width: 200, title: '记录时间'}
                    , {title: '操作', width: 100, minWidth: 50, align: 'center', fixed: 'right', toolbar: '#table-operation' }
                ]
            ]
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , page: true
            , limit: 10
            , limits: [10, 15, 20, 25, 30]
            , text: {
                none: '暂无相关数据'
            }
            , done: function (res) {
                layer.closeAll('loading');
                admin.exit(res);
            }
        });

        //监听工具条
        table.on('tool(lay-list)', function (obj) {
            var data = obj.data;
            // 删除
            if (obj.event === 'delete') {
                layer.confirm('确定删除？', {icon: 3, title: '确认删除'}, function (index) {
                    admin.req({
                        url: 'delete/' + data.id
                        , done: function (res) {
                            layer.close(index);
                            //数据刷新
                            table.reload('lay-list');
                            layer.msg(res.msg, {icon: 1});
                        }
                    });
                });
            }
        });

        // 点击事件触发
        $('.layui-btn.layuiadmin-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //事件
        var active = {
            batchDel: function () {
                var data = table.checkStatus('lay-list').data;
                if (data.length === 0) {
                    return layer.msg('请选择数据', {icon: 2});
                }
                var ids = [];
                $.each(data, function(k, v) {
                    ids.push(v.id);
                });
                layer.confirm('确定删除？', {icon: 3, title: '确认删除'}, function (index) {
                    admin.req({
                        url: 'delete/' + ids
                        , done: function (res) {
                            layer.close(index);
                            //数据刷新
                            table.reload('lay-list');
                            layer.msg(res.msg, {icon: 1});
                        }
                    });
                });
            }
        }
    });
</script>
</body>
</html>
